<?php

/**
 * ECSHOP mobile首页
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liuhui $
 * $Id: index.php 15013 2010-03-25 09:31:42Z liuhui $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

	/* 广告 */
	$sql = "SELECT ad_link,ad_code FROM " .  $GLOBALS['ecs']->table('ad')  . ' ORDER BY end_time DESC';
	$ad = $db->getAll($sql);	
	$smarty->assign('ad',$ad);

	/*获取分类轮播信息*/
	$sql = "SELECT * FROM " .  $GLOBALS['ecs']->table('category'). " WHERE  is_open = '1' ";
	$result = $db->getAll($sql);
	// print_r($result);
	// die;
	if(!empty($result)){
		$smarty->assign('valuea',$result);
	}

/*zy*/
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'default';
/*显示数量*/
$page_size = 1;

/*默认*/
if($act == 'default'){

    /*活动数量*/
	$sql = "SELECT count(*) FROM " . $GLOBALS['ecs']->table('activity');
    $counts = $db->getOne($sql);

	/*分页数*/
	$page_count = $counts > 0 ? ceil($counts / $page_size) : 1;

	$smarty->assign('page_count', $page_count);
	
	/* y-查询活动列表 k*/
	$sql = "SELECT * FROM " .  $GLOBALS['ecs']->table('activity')  . " ORDER BY endsign_time DESC limit 0,".$page_size;
	
	$result = $db->getAll($sql);
	
	if(!empty($result)){

		foreach($result as $k=>$value){
			
			/*活动状态转换*/
			/*当前时间获取*/
			$showtime=date("Y-m-d H:i");
			$NowTime = !empty($showtime) ? local_strtotime($showtime) : 0;

			if($NowTime < $value['startsign_time']){

				$result[$k]['activity_status'] = '1';

			}elseif ($value['endsign_time'] > $NowTime && $NowTime > $value['startsign_time']) {

				$result[$k]['activity_status'] = '2';

			}elseif ($value['activity_stime'] > $NowTime && $NowTime > $value['startsign_time']) {	

				$result[$k]['activity_status'] = '4';

			}elseif ($value['activity_etime'] > $NowTime && $NowTime > $value['activity_stime']) {

				$result[$k]['activity_status'] = '3';

			}elseif ($NowTime > $value['activity_etime']) {	

				$result[$k]['activity_status'] = '5';

			}else{	

				$result[$k]['activity_status'] = '0';

			}




			/*时间格式化*/
			$result[$k]['activity_stime'] = local_date('Y-m-d',$value['activity_stime']);
			
			$result[$k]['activity_etime'] = local_date('Y-m-d',$value['activity_etime']);
			$result[$k]['endsign_time'] = local_date('Y-m-d H:i', $value['endsign_time']);

		} 
	}

	$smarty->assign('value', $result);
	/* y-查询活动列表 j*/
	$smarty->display("index.dwt");
	
}
 
/*ajax分页*/
if($act == 'ajax_query'){
	
	$ret = array();
    $page = $_POST['page'];
	
    $where1 = isset($_REQUEST['where1']) ? $_REQUEST['where1'] : '';
    $where2 = isset($_REQUEST['where2']) ? $_REQUEST['where2'] : '';
    $where3 = isset($_REQUEST['where3']) ? $_REQUEST['where3'] : '';
    
	$where = ' where 1 ';
	
    if(!empty($where1)){
		$where .=  " and xxx = ".$where1;
	}
	
	if(!empty($where2)){
		$where .=  " and xxx = ".$where2;
	}
	
	if(!empty($where3)){
		$where .=  " and xxx = ".$where3;
	}
	
	
	/*查询数据*/
	$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('activity') . $where . " ORDER BY endsign_time DESC limit " . ($page+1)*$page_size .",".$page_size;
	
    $result= $db->getAll($sql);

	if(!empty($result)){

		foreach($result as $k=>$value){
			
			/*活动状态转换*/
			/*当前时间获取*/
			$showtime=date("Y-m-d H:i");
			$NowTime = !empty($showtime) ? local_strtotime($showtime) : 0;
			
			if($NowTime < $value['startsign_time']){

				$result[$k]['activity_status'] = '1';

			}elseif ($value['endsign_time'] > $NowTime && $NowTime > $value['startsign_time']) {

				$result[$k]['activity_status'] = '2';

			}elseif ($value['activity_stime'] > $NowTime && $NowTime > $value['startsign_time']) {	

				$result[$k]['activity_status'] = '4';

			}elseif ($value['activity_etime'] > $NowTime && $NowTime > $value['activity_stime']) {

				$result[$k]['activity_status'] = '3';

			}elseif ($NowTime > $value['activity_etime']) {	

				$result[$k]['activity_status'] = '5';

			}else{	

				$result[$k]['activity_status'] = '0';

			}
			$result[$k]['activity_img'] = "../admin/".$value['activity_img'];

			/*时间格式化*/
			$result[$k]['activity_stime'] = local_date('Y-m-d',$value['activity_stime']);
			
			$result[$k]['activity_etime'] = local_date('Y-m-d',$value['activity_etime']);
			$result[$k]['endsign_time'] = local_date('Y-m-d H:i', $value['endsign_time']);

		} 
	}
	
	/*ajax返回值*/
	$smarty->assign('result', $result);
	$ret['page'] = $page+1;
	
	$html = "";
	
	foreach($result as $key=>$value){
		
		$html .="<li class='mui-table-view-cell mui-media'><a href='goods.php?activity_id=".$value['activity_id']."'><img class='mui-media-object mui-pull-left' src='".$value['activity_img']."'><div class='mui-media-body'><h3 class='mui-ellipsis-2' >".$value['activity_name']."</h3><div class='adds' ><i class='icons-all icon_adds'></i>地点：<span>".$value['activity_place']."</span></div><div class='adds' ><i class='icons-all icon_date'></i>".$value['activity_stime']."至".$value['activity_stime']."</div><div class='tfont'>报名结束时间：".$value['endsign_time']."</div><div class='tfont'><i class='icons-all icon_eye'></i><span id='num'>(".$value['number'].")</span>";
		
		
		if($value['activity_status'] == '1'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#ff5842'>预热中</span>";
			
		}elseif($value['activity_status'] == '2'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#98cb15'>报名中</span>";
			
		}elseif($value['activity_status'] == '3'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#109646'>活动中</span>";

			
		}elseif($value['activity_status'] == '4'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#0CD0E4'>报名结束</span>";
			
		}elseif($value['activity_status'] == '5'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#b3b3b3'>活动结束</span>";
			
		}else{
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#FF7600'>未设置</span>";
			
		}
			
		$html .="</div></div></a></li>";
		
	}
	
	$ret['info'] = $html;

    die(json_encode($ret));

}

?>
